package org.jianfa.homework.mail.service.impl;

import org.apache.dubbo.config.annotation.Service;
import org.jianfa.homework.api.MailSerivce;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

import javax.mail.internet.MimeMessage;

@Service
public class MailServiceImpl implements MailSerivce {

    private static final Logger logger = LoggerFactory.getLogger(MailServiceImpl.class);

    @Autowired
    private JavaMailSender javaMailSender;

    @Override
    public boolean sendMail(String email, String code) {

        try {
            MimeMessage mimeMessage = javaMailSender.createMimeMessage();
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage);
            mimeMessageHelper.setFrom("linzh007@126.com");
            mimeMessageHelper.setTo(email);
            mimeMessageHelper.setSubject("验证码");
            mimeMessageHelper.setText(code);

            javaMailSender.send(mimeMessage);

            return true;
        } catch (Exception ex) {
            logger.error("发送验证码失败", ex);
            return false;
        }
    }
}
